package com.clx.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clx.yygh.common.result.Result;
import com.clx.yygh.common.utils.MD5;
import com.clx.yygh.hosp.service.HospitalSetService;
import com.clx.yygh.model.hosp.HospitalSet;
import com.clx.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/HospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有医院信息")
    @GetMapping("/findAll")
    public Result findAll(){
        return Result.ok(hospitalSetService.list());
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除医院信息")
    @DeleteMapping("/delete/{id}")
    public Result removeById(@PathVariable Long id){
        boolean flg = hospitalSetService.removeById(id);
        if (!flg) return Result.fail();
        return Result.ok();
    }

    /**
     * 分页查询
     * @param current
     * @param limit
     * @param hospitalSetQueryVo
     * @return
     */
    @ApiOperation(value = "分页查询医院设置信息")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable Long current,   //当前页
                                  @PathVariable Long limit,     //每页记录数
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){   //封装查询条件 ,required = false 表示可以为空
        Page<HospitalSet> page = new Page<>(current,limit);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (StringUtils.isNotBlank(hosname)) {
            queryWrapper.like("hosname", hosname);
        }
        if (StringUtils.isNotBlank(hoscode)) {
            queryWrapper.eq("hoscode", hoscode);
        }
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page,queryWrapper);

        return Result.ok(hospitalSetPage);
    }

    @ApiOperation(value = "保存医院设置信息")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //状态：1-可用  0-不可用
        hospitalSet.setStatus(1);

        //设置密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+ "" + random.nextInt(1000)));

        boolean flg = hospitalSetService.save(hospitalSet);

        if (flg) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    @ApiOperation(value = "根据ID查询医院设置信息")
    @RequestMapping("/findHospitalSetById/{id}")
    public Result findHospitalSetById(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    @ApiOperation(value = "更新医院设置信息")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flg = hospitalSetService.updateById(hospitalSet);
        if (flg) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "批量删除医院设置信息")
    @DeleteMapping("/batchRemoveHospSet")
    public Result batchRemoveHospSet(@RequestBody List<Long> ids){
        boolean flg = hospitalSetService.removeByIds(ids);
        if (flg) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "锁定")
    @PutMapping("/lockHospSet/{id}/{status}")
    public Result lockHospSet(@PathVariable Long id,
                              @PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @ApiOperation(value = "发送签名密钥")
    @PutMapping("/sendSignKey/{id}")
    public Result sendSignKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();

        // TODO 发送短信
        return Result.ok();
    }

}
